<?php
require_once("../../resources/commons.php");
require_once("../../data/User.php");
require_once("../../data/Activity.php");

function displayDetailsLink($p_record){
	?>
	<a href="profile_display.php?id=<?=base64_encode($p_record->getId()) ?>">Details</a>
	<?php
}

/**
 * This function displays the profile of a user.<br>
 * The second parameter of the function is used to "forge" an URL which will
 * take as a parameter the manager or the team members user IDs in order to display their profile.<br>
 * The detail form URL must take "id" as a request parameter.<br>
 * This parameter must contain the base64 encoded userId of the user to be displayed.
 * @param string $p_userId the user ID of the user to be displayed
 * @param boolean $p_isDisplayTeamTable true if the team members table must be displayed
 */
function displayUserProfile($p_userId, $p_isDisplayTeamTable){
	$l_user=User::getFromPrimaryKey($p_userId);
	?>
<table>
	<tr>
		<td><b>First name: </b></td>
		<td><?=htmlentities($l_user->getFirstName(),ENT_QUOTES); ?></td>
	</tr>
	<tr>
		<td><b>Last name: </b></td>
		<td><?=$l_user->getLastName() ?></td>
	</tr>
	<tr>
		<td><b>E-mail: </b></td>
		<td><a href="mailto:<?=$l_user->getEmail() ?>"><?=$l_user->getEmail() ?></a></td>
	</tr>
	<tr>
	<?php
	$l_manager=$l_user->getManagerUser_id_Ref();
	if(isset($l_manager)){
		?>
		<td><b>Manager: </b></td>
		<td><a
			href="profile_display.php?id=<?=base64_encode($l_user->getManagerUser_id()) ?>"><?=$l_manager->getFirstName() . " " . $l_manager->getLastName() ?></a></td>
			<?php
}else{
	?>
		<td><b>Manager: </b></td>
		<td>None</td>
		<?php
}
?>
	</tr>
	<?php
	//Feed link is proviate and must be only displayed to the authorized user
	if($p_userId==SessionManager::getCurrentLoggedUserId()){
	?>
	<tr>
	<td><b>Action feed</b></td>
	<?php
		//RSS feeds are available without HTTPS
		$l_rssRootPath="http://" . $_SERVER["SERVER_NAME"] . ROOT_PATH;
	?>
	<td><a href="<?=$l_rssRootPath ?>/rss/dashboard_rss.php?fk=<?=$l_user->getFeedKey() ?>"><img src="../../resources/images/rss.gif" border="0"/></a></td>
	</tr>
	<?php
	}
	?>
<?php
	if($p_isDisplayTeamTable){
		$tableData=new TableData("TeamMembersTable");
		$records=User::getFromCustomCriteria(
			"WHERE ManagerUser_id=:ManagerUser_id",
		array("ManagerUser_id" => $l_user->getId()));
		if(count($records)!=0){
?>
	<tr>
		<td><b>Team members:</b></td>
		<td>
		</td>
	</tr>
	<tr>
		<td colspan="2">
		<?php		
			$tableData->setRecords($records);
			$tableData->addHeader("firstName","First name");
			$tableData->addHeader("lastName","Last name");
			$tableData->addHeader("email","E-mail");
			$tableData->addAction("displayDetailsLink");
	        echo displayTable($tableData);
		}
		?>
		</td>
	</tr>
	<tr>
		<td valign="top"><b>Admin rights:</b></td>
		<td>
		<ul>
		<?php
			if($l_user->getIsAdmin()){
				echo "<li>Super admin</li>";
			}
			if($l_user->getIsFinanceAdmin()){
				echo "<li>Finance admin</li>";
			}
			if(!($l_user->getIsAdmin()) && !($l_user->getIsFinanceAdmin())){
				echo "none";
			}	
		?>
		</ul>
		</td>
	</tr>
	<tr>
		<td valign="top"><b>Activities:</b></td>
		<td>
		<?php
		$l_activities=$l_user->getActivitys();
		if(count($l_activities)==0){
			echo "none";
		}else{
			$l_activitiesTable=new TableData("ActivitiesTable");
			$l_activitiesTable->addHeader("title","Title");
			$l_activitiesTable->addHeader("activityTypeLabel","Type");
			$l_activitiesTable->addHeader("isArchivedStatus","Archived");
			$l_activitiesTable->setRecords($l_activities);
			displayTable($l_activitiesTable);
		}
		?>
		</td>
	</tr>
<?php
	}
?>
</table>
<?php
	}
?>